package com.Questions.elseQuestion;

import java.util.*;
import java.util.ArrayList;

/**
 * z字形变换
 */
public class leetcode6 {
    public String convert(String s, int numRows) {
        if(numRows == 1){
            return s;
        }
        List<StringBuffer> list = new ArrayList<StringBuffer>();
        for (int i = 0; i < numRows; i++) {
            list.add(new StringBuffer());
        }
        int index = 0;// 要加入到哪一个列表中
        boolean isAdd = false;
        for (int i = 0; i < s.length(); ++i) {
            list.get(index).append(s.charAt(i));
            if (index == numRows - 1 || index == 0){
                isAdd = !isAdd;
            }
            if(isAdd){
                index++;
            }else{
                index--;
            }
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < numRows; i++) {
            sb.append(list.get(i));
        }
        return sb.toString();
    }
}
